package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.youdianstar.app.common.ConfigKey
import com.youdianstar.app.common.widget.RadiusImageView
import com.youdianstar.app.R
import com.youdianstar.app.util.DataAppManager


/**
 * File: ShopImgEdtAdapter
 * Author: 82149 Create: 2023/8/3 19:06
 * Changes (from 2023/8/3)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class ShopImgEdtAdapter(val context: Context, val dataList: MutableList<String>, val callback : OnPicSelect) :RecyclerView.Adapter<ShopImgEdtAdapter.PicHolder>() {
    class PicHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val ivIcon = itemView.findViewById<RadiusImageView>(R.id.iv_shop)
        val ivDelete = itemView.findViewById<ImageView>(R.id.iv_delete)
    }

    interface OnPicSelect {
        fun onAdd()

        fun onDelete(index:Int)

        fun onItemClick(url:String)
    }

    private var isEditModel = false
    private var host = ""
    private val max :Int = 5

    init {
        this.host = DataAppManager.instance.getConfig(ConfigKey.FILE_HOST)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PicHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_item_image_grid, parent, false)
        return PicHolder(view)
    }

    override fun getItemCount(): Int {
        val count: Int = if (dataList.size == 0) 1 else dataList.size + 1
        return if (count > max)
            dataList.size
        else
            count
    }

    override fun onBindViewHolder(holder: PicHolder, position: Int) {
        val isMax = dataList.size>=max
        if (!isMax && position==0){
            holder.ivDelete.visibility = View.INVISIBLE
            holder.ivIcon.load(R.mipmap.mc_image_add){

            }
            holder.ivIcon.setOnClickListener {
                if (isEditModel)
                    callback.onAdd()
            }
        } else {
            //已经达到最大, 从0开始 else 从1开始
            val index = if (isMax) position else position-1
            if (index>=0 && index<dataList.size){
                dataList[index].let {
                    val pathStr = if(it.contains("http")) it else host+it
                    holder.ivIcon.load(pathStr){
                        error(R.drawable.ic_img_picker_default)
                    }

                    holder.ivIcon.setOnClickListener {
                        callback.onItemClick(pathStr)
                    }
                }
                holder.ivDelete.visibility = if (isEditModel) View.VISIBLE else View.INVISIBLE
                holder.ivDelete.setOnClickListener {
                    if (isEditModel)
                        callback.onDelete(index)
                }
            }
        }
    }

    /**刷新数据*/
    fun refreshData(list: List<String>,isEdit :Boolean) {
        dataList.clear()
        dataList.addAll(list)
        isEditModel = isEdit
        notifyDataSetChanged()
    }
}